package com.ps.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class PaginationSupport<T> {
	 //表示一页有多少行
	 public final static int PAGESIZE = 5;  
	 public static final String PAGE_INDEX = "PAGE_INDEX";
	 public static final String PAGE_SIZE = "PAGE_SIZE";
	   
     private int pageSize = PAGESIZE;  
   
     private List<T> items;  
   
     private int totalCount;  
   
     private int[] indexes = new int[0];  
     
     private int currentPageIndex;
   
     private int startIndex = 0;  
     
     private String pageInfo;
   
     public PaginationSupport(List<T> items, int totalCount) {  
         setPageSize(PAGESIZE);  
         setTotalCount(totalCount);  
         setItems(items);          
         setStartIndex(0);  
         currentPageIndex = getCurrentPageIndex();
         generatePageInfo();
     }  
   
     public PaginationSupport(List<T> items, int totalCount, int startIndex) {  
         setPageSize(PAGESIZE);  
         setTotalCount(totalCount);  
         setItems(items);          
         setStartIndex(startIndex);  
         currentPageIndex = getCurrentPageIndex();
         generatePageInfo();
     }  
   
     public PaginationSupport(List<T> items, int totalCount, int pageSize, int startIndex) {  
                 setPageSize(pageSize);  
         setTotalCount(totalCount);  
         setItems(items);  
         setStartIndex(startIndex);  
         currentPageIndex = getCurrentPageIndex();
         generatePageInfo();
     }  
   /**
    * 得到已分页好的结果集 
    * 
    * @return java.util.List
    */
     public List<T> getItems() {  
         return items;  
     }
     public void setItems(List<T> items) {  
         this.items = items;  
     }  
   
     public int getPageSize() {  
         return pageSize;  
     }  
    
     public void setPageSize(int pageSize) {  
         this.pageSize = pageSize;  
     }  
   /**
    * 得到总结果数
    * 
    * @return int
    */
     public int getTotalCount() {  
         return totalCount;  
     }  
   
     public void setTotalCount(int totalCount) {  
         if (totalCount > 0) {  
             this.totalCount = totalCount;  
             int count = totalCount / pageSize;  
             if (totalCount % pageSize > 0)  
                 count++;  
             indexes = new int[count];  
             for (int i = 0; i < count; i++) {  
                 indexes[i] = pageSize * i;  
             }  
         } else {  
             this.totalCount = 0;  
         }  
     }  
   /**
    *  得到分页索引的数组 
    * 
    * @return int[]
    */
     public int[] getIndexes() {  
         return indexes;  
     }  
   
     public void setIndexes(int[] indexes) {  
         this.indexes = indexes;  
     }  
   /**
    * 当前分页索引
    * @return int
    */
     public int getStartIndex() {  
         return startIndex;  
     }  
   
     public void setStartIndex(int startIndex) {  
         if (totalCount <= 0)  
             this.startIndex = 0;  
         else if (startIndex >= totalCount)  
             this.startIndex = indexes[indexes.length - 1];  
         else if (startIndex < 0)  
             this.startIndex = 0;  
         else {  
             this.startIndex = indexes[startIndex / pageSize];  
         }  
     }  
   /**
    * 下一页索引 
    * @return int
    */
     public int getNextIndex() {  
         int nextIndex = getStartIndex() + pageSize;  
         if (nextIndex >= totalCount)  
             return getStartIndex();  
         else  
             return nextIndex;  
     }  
   /**
    * 上一页索引  
    * 
    * @return int
    */
     public int getPreviousIndex() {  
         int previousIndex = getStartIndex() - pageSize;  
         if (previousIndex < 0)  
             return 0;  
         else  
             return previousIndex;  
     }


	


	public int getCurrentPageIndex() {		
		return Arrays.binarySearch(this.indexes,this.startIndex);
	}

	public String getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(String pageInfo) {
		this.pageInfo = pageInfo;
	}  

    private void generatePageInfo() {
    	this.pageInfo = "共有&nbsp;" 
            + this.totalCount + "&nbsp;条数据&nbsp;&nbsp;"
            + "每页&nbsp;" + this.pageSize + "&nbsp;条&nbsp;当前&nbsp;"
            + (this.currentPageIndex + 1) + "/" + this.indexes.length + "页";
    }
	
}
